package org.mtmi.ui.controls.popups;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.GraphicUtil;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.ITouchableControl;
import org.mtmi.ui.controls.IVisualControl;
import org.mtmi.ui.controls.TouchControlEvent;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.scenes.COLOR;
import org.mtmi.ui.scenes.FontManager;
import org.mtmi.ui.scenes.Scene;

/**
 * Basement class used for pop up controls.
 * Pop up controls cover the whole surface of the scene and thus, they hide all controls 
 * that were present on the scene before the pop up opens.
 * 
 * Those controls are useful to catch user's attention given focus to a small set of particular 
 * controls (eg. one question and buttons to answer). 
 * Those controls are permanent since they hide the scene.
 * 
 * Subclass should implement {@link #paintContent(GC)} which is in charge of drawing the specific content 
 * of the pop up without the background.
 *  
 * @author jgout
 *
 */
public abstract class Popup extends Control implements IVisualControl, ITouchableControl {

	private static final int DEFAULT_ALPHA = 157;
	private int alpha;
	
	public Popup(Scene scene, int flags ) {
		this( scene, DEFAULT_ALPHA, flags );
	}

	public Popup(Scene scene, int alpha, int flags ) {
		super( scene, flags );
		this.alpha = alpha;
		setSize(scene.getSize());
		redraw();
	}
	
	@Override
	public Point computeSize(LayoutData l) {
		return getScene().getSize();
	}

	/**
	 * Returns the dimension of the given string in the context of the pop up (font used).
	 * @param s the text to measure
	 * @return the dimension of the given string
	 */
	protected Point getSize(String s) {
		GC gc = new GC(getScene());
		gc.setFont(FontManager.getInstance().getNormalFont());
		Point point = gc.textExtent(s);
		gc.dispose();
		return point;
	}
	
	/**
	 * Facility method.
	 * Return the given string with additional CR inside.
	 * @param text
	 * @return the multi line version of the given text according to the width of the pop up
	 */
	protected String cutText(String text) {
		int limit = getScene().getSize().x/2;
		String[] words = text.split("\\s"); //$NON-NLS-1$
		String res = words[0];
		for (int i = 1; i < words.length; i++) {
			if(getSize(res+' '+words[i]).x > limit) {
				res+='\n';
			} else {
				res += ' ';
			}
			res += words[i];
		}
		return res;
	}
	
	@Override
	public final void paint(GC gc) {
		//- opacity
		gc.setBackground( COLOR.sceneBg );
		gc.setAlpha(alpha);
		int w = getSize().x;
		int h = getSize().y;
		gc.fillRectangle(0, 0, w, h);
		gc.setAlpha(255);
		//- paint the specific content of the pop up.
		paintContent(gc, w, h);
	}

	@Override
	public boolean containsTouch(TouchControlEvent e) {
		return GraphicUtil.rectContains( 0, 0, size.x, size.y, e.x, e.y );
	}
	
	public abstract void paintContent(GC gc, int width, int height);
}
